package com.cloud.common.base.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author wangjian
 * @version 1.0.0.RELEASES
 * @create 2021年03月16日 3/16/21
 * @since JDK 1.8
 * Life is so short,do something to make yourself happy,such as coding
 */
public class PageVO<T> implements Serializable {

    /**
     * serialVersionUID,非版本变更，否则不可随意变更
     */
    private static final long serialVersionUID = 8493643462901159303L;

    /**
     * 总行数
     */
    @JSONField(name = "totalCount")
    @JsonProperty("totalCount")
    private Long totalCount;

    /**
     * 数据列表
     */
    @JSONField(name = "list")
    @JsonProperty("list")
    private List<T> list;

    public PageVO() {
    }

    /**
     * 分页
     */
    public PageVO(IPage<T> page) {
        this.list = Optional.ofNullable(page.getRecords()).orElse(Collections.emptyList());
        this.totalCount = page.getTotal();
    }

    @Override
    public String toString() {
        return "PageVO{" +
                "totalCount=" + totalCount +
                ", list=" + list +
                '}';
    }

    public Long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Long totalCount) {
        this.totalCount = totalCount;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
